package com.ruoyi.web.controller.cooperative;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.GoodsSpuAndCategoryNameVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GoodsSpu;
import com.ruoyi.system.service.IGoodsSpuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品的基本属性Controller
 * 
 * @author ruoyi
 * @date 2022-01-25
 */
@RestController
@RequestMapping("/system/spu")
public class GoodsSpuController extends BaseController
{
    @Autowired
    private IGoodsSpuService goodsSpuService;
    /**
     * 查询商品的基本属性列表 带上类别的名字
     */
    @PreAuthorize("@ss.hasPermi('system:spu:list')")
    @GetMapping("/listAndCategoryName")
    public TableDataInfo listAndCategoryName(GoodsSpuAndCategoryNameVo goodsSpuAndCategoryNameVo)
    {
        startPage();
        List<GoodsSpuAndCategoryNameVo> list = goodsSpuService.selectGoodsSpuListAndCategoryName(goodsSpuAndCategoryNameVo);
        return getDataTable(list);
    }


    /**
     * 查询商品的基本属性列表
     */
    @PreAuthorize("@ss.hasPermi('system:spu:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSpu goodsSpu)
    {
        startPage();
        List<GoodsSpu> list = goodsSpuService.selectGoodsSpuList(goodsSpu);
        return getDataTable(list);
    }

    /**
     * 导出商品的基本属性列表
     */
    @PreAuthorize("@ss.hasPermi('system:spu:export')")
    @Log(title = "商品的基本属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsSpu goodsSpu)
    {
        List<GoodsSpu> list = goodsSpuService.selectGoodsSpuList(goodsSpu);
        ExcelUtil<GoodsSpu> util = new ExcelUtil<GoodsSpu>(GoodsSpu.class);
        util.exportExcel(response, list, "商品的基本属性数据");
    }

    /**
     * 获取商品的基本属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:spu:query')")
    @GetMapping(value = "/{goodSpuId}")
    public AjaxResult getInfo(@PathVariable("goodSpuId") Long goodSpuId)
    {
        return AjaxResult.success(goodsSpuService.selectGoodsSpuByGoodSpuId(goodSpuId));
    }

    /**
     * 新增商品的基本属性
     */
    @PreAuthorize("@ss.hasPermi('system:spu:add')")
    @Log(title = "商品的基本属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsSpu goodsSpu)
    {
        return toAjax(goodsSpuService.insertGoodsSpu(goodsSpu));
    }

    /**
     * 修改商品的基本属性
     */
    @PreAuthorize("@ss.hasPermi('system:spu:edit')")
    @Log(title = "商品的基本属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsSpu goodsSpu)
    {
        return toAjax(goodsSpuService.updateGoodsSpu(goodsSpu));
    }

    /**
     * 删除商品的基本属性
     */
    @PreAuthorize("@ss.hasPermi('system:spu:remove')")
    @Log(title = "商品的基本属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodSpuIds}")
    public AjaxResult remove(@PathVariable Long[] goodSpuIds)
    {
        return toAjax(goodsSpuService.deleteGoodsSpuByGoodSpuIds(goodSpuIds));
    }
}
